﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using RiskMan.Config;
using RiskMan.DataClass;
using RiskMan.DataClass.Gerchik;

namespace RiskMan.Views.Robot
{
    /// <summary>
    /// Логика взаимодействия для addNewGerchikRobot.xaml
    /// </summary>
    public partial class AddNewGerchikRobot : Window
    {
        Data data;
        GechikRobotItem itemGerichikRobot;

        public AddNewGerchikRobot(GechikRobotItem item, Data data, bool isedit)
        {
            InitializeComponent();



            this.data = data;
            itemGerichikRobot = item;

            if (item.ListLevel == null)
                item.ListLevel = new List<double>();
            item.GuidLevelItem = Guid.NewGuid();

            //var dfd = data.GetClientCode();
            cmbxClientCode.ItemsSource = data.GetClientCode();
            cmbxClientCode.SelectedIndex = 0;
            if (String.IsNullOrEmpty(item.ClientCode))
                cmbxClientCode.SelectedValue = item.ClientCode;

            cmbxMarket.ItemsSource = data.GetMarkets();
            cmbxMarket.SelectedIndex = 0;
            if (item.Market != null)
                cmbxMarket.SelectedValue = item.Market;

            if (!String.IsNullOrEmpty(cmbxMarket.Text))
            {
                cmbxInstrument.ItemsSource = data.GetInstrumets(cmbxMarket.Text);
                cmbxInstrument.SelectedIndex = 0;
            }
            if (item.Instrument != null)
                cmbxInstrument.SelectedValue = item.Instrument;

            cmbxTimeFrame.ItemsSource = data.GetTimeFrame();
            cmbxTimeFrame.SelectedIndex = 0;
            if (item.TimeFrame != null)
                cmbxTimeFrame.SelectedValue = item.TimeFrame;

            txbxThirdBarDeviation.Text = item.ThirdBarDeviation.ToString();
            txbxKillOrderPoint.Text = item.KillOrderPoint.ToString();
            txbxStopLimiOtstup.Text = item.StopLimiOtstup.ToString();
            txbxBreakeven.Text = item.Breakeven.ToString();
            txbxProfit.Text = item.Profit.ToString();
            txbxMaxExcess.Text = item.MaxExcess.ToString();

            txbxTimeStartLevel.Text = item.TimeStartLevel.TimeOfDay.ToString();
            txbxTimeStopLevel.Text = item.TimeStopLevel.TimeOfDay.ToString();
            txbxTimeClosePoz.Text = item.TimeClosePoz.TimeOfDay.ToString();
            txbxQuantity.Text = item.Quantity.ToString();
            txbxAttemptAmount.Text = item.AttemptAmount.ToString();

            ChbxSoundAlert.IsChecked = item.SoundAlert;
            cmbxTypeRobot.ItemsSource = new List<string>() { Cfg.TypeGerchikRobot, Cfg.TypeGerchikSovetnik };
            cmbxTypeRobot.SelectedIndex = 0;
            if (item.TypeRobot != null)
                cmbxTypeRobot.SelectedValue = item.TypeRobot;


            ChbxReapetEnterOnLevel.IsChecked = item.ReapetEnterOnLevel;


            if (itemGerichikRobot.Patterns == null)
            {
                itemGerichikRobot.Patterns = new List<string>();
                foreach (var itemPattern in data.DictionaryPattern)
                {
                    LbxTradePattern.Items.Add(itemPattern.Key);
                    itemGerichikRobot.Patterns.Add(itemPattern.Key);
                }
            }
            else
            {
                foreach (var itemPattern in data.DictionaryPattern)
                {
                    LbxPattern.Items.Add(itemPattern.Key);
                }
            }
            foreach (var itemPattern in itemGerichikRobot.Patterns)
            {
                LbxTradePattern.Items.Add(itemPattern);
                LbxPattern.Items.Remove(itemPattern);
            }

        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                itemGerichikRobot.ClientCode = cmbxClientCode.Text;
                itemGerichikRobot.Market = cmbxMarket.Text;
                itemGerichikRobot.Instrument = cmbxInstrument.Text;
                itemGerichikRobot.TimeFrame = cmbxTimeFrame.Text;

                itemGerichikRobot.ThirdBarDeviation = Convert.ToDouble(txbxThirdBarDeviation.Text);
                itemGerichikRobot.KillOrderPoint = Convert.ToDouble(txbxKillOrderPoint.Text);
                itemGerichikRobot.StopLimiOtstup = Convert.ToDouble(txbxStopLimiOtstup.Text);
                itemGerichikRobot.Profit = Convert.ToDouble(txbxProfit.Text);
                itemGerichikRobot.MaxExcess = Convert.ToDouble(txbxMaxExcess.Text);
                itemGerichikRobot.Breakeven = Convert.ToDouble(txbxBreakeven.Text);
                itemGerichikRobot.Quantity = Convert.ToInt32(txbxQuantity.Text);

                if (itemGerichikRobot.TimeStartLevel != Convert.ToDateTime(txbxTimeStartLevel.Text))
                    itemGerichikRobot.CommentForFirstCandle = false;

                itemGerichikRobot.TimeStartLevel = Convert.ToDateTime(txbxTimeStartLevel.Text);
                itemGerichikRobot.TimeStopLevel = Convert.ToDateTime(txbxTimeStopLevel.Text);
                itemGerichikRobot.TimeClosePoz = Convert.ToDateTime(txbxTimeClosePoz.Text);
                itemGerichikRobot.AttemptAmount = Convert.ToInt32(txbxAttemptAmount.Text);

                itemGerichikRobot.ReapetEnterOnLevel = Convert.ToBoolean(ChbxReapetEnterOnLevel.IsChecked);
                itemGerichikRobot.SoundAlert = Convert.ToBoolean(ChbxSoundAlert.IsChecked);
                itemGerichikRobot.TypeRobot = cmbxTypeRobot.Text;

                Close();
            }
            catch
            { }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }



        private void cmbxClientCode_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void cmbxMarket_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (cmbxMarket.Items.Count > 0)
                    if (!String.IsNullOrEmpty(cmbxMarket.SelectedItem.ToString()))
                    {

                        cmbxInstrument.ItemsSource = data.GetInstrumets(cmbxMarket.SelectedItem.ToString());
                        cmbxInstrument.SelectedIndex = 0;
                    }
            }
            catch
            { }
        }

        private void cmbxInstrument_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }


        private void txbx_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (!Char.IsDigit(e.Text, 0))
                e.Handled = true;

            if ((e.Text.IndexOf('.') == 0 || e.Text.IndexOf(',') == 0) && ((TextBox)sender).Text.IndexOf('.') == -1 && ((TextBox)sender).Text.IndexOf(',') == -1)
                e.Handled = false;


        }

        private void txbx_GotFocus(object sender, RoutedEventArgs e)
        {

            try
            {


                if (((TextBox)sender).Text != "")
                {
                    if (Convert.ToDouble(((TextBox)sender).Text) == 0)
                    {
                        ((TextBox)sender).Text = "";
                    }

                }
            }
            catch
            {
                ((TextBox)sender).Text = "";
            }
        }

        private void txbxDitgit_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (!Char.IsDigit(e.Text, 0))
                e.Handled = true;
        }

        private void txbx_LostFocus(object sender, RoutedEventArgs e)
        {
            if (((TextBox)sender).Text == "")
                ((TextBox)sender).Text = 0.ToString();

            char PointChar = ',';
            string str = (1.2).ToString();
            if (str.IndexOf('.') > 0) PointChar = '.';

            if (PointChar == ',')
                ((TextBox)sender).Text = ((TextBox)sender).Text.Replace('.', PointChar);

            if (PointChar == '.')
                ((TextBox)sender).Text = ((TextBox)sender).Text.Replace(',', PointChar);


            //Char PointChar = Convert.ToChar(System.Globalization.CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator);


            //if (PointChar == '.')
            //    return Convert.ToDouble(str.Replace(',', '.'));
            //return Convert.ToDouble(str.Replace('.', ','));
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ((Button)sender).Focusable = false;
            SoundPanel soundPanel = new SoundPanel();
            soundPanel.ShowDialog();
        }

        private void Button_AddOne_Click(object sender, RoutedEventArgs e)
        {
            if (LbxPattern.SelectedItem != null)
            {
                foreach (var item in data.DictionaryPattern)
                {
                    if (LbxPattern.SelectedItem.ToString() == item.Key)
                    {
                        itemGerichikRobot.Patterns.Add(item.Key);
                        break;
                    }
                }
                LbxTradePattern.Items.Add(LbxPattern.SelectedItem);
                LbxPattern.Items.Remove(LbxPattern.SelectedItem);
                LbxTradePattern.Items.SortDescriptions.Add(new SortDescription("", ListSortDirection.Ascending));
            }
        }

        private void Button_DellOne_Click(object sender, RoutedEventArgs e)
        {
            if (LbxTradePattern.SelectedItem != null)
            {
                foreach (var item in data.DictionaryPattern)
                {
                    if (LbxTradePattern.SelectedItem.ToString() == item.Key)
                    {
                        itemGerichikRobot.Patterns.Remove(item.Key);
                        break;
                    }
                }
                LbxPattern.Items.Add(LbxTradePattern.SelectedItem);
                LbxTradePattern.Items.Remove(LbxTradePattern.SelectedItem);
                LbxPattern.Items.SortDescriptions.Add(new SortDescription("", ListSortDirection.Ascending));
            }
        }

        private void Button_DellAll_Click(object sender, RoutedEventArgs e)
        {
            itemGerichikRobot.Patterns.Clear();
            LbxPattern.Items.Clear();
            foreach (var itemPattern in data.DictionaryPattern)
            {
                LbxPattern.Items.Add(itemPattern.Key);
            }

            LbxTradePattern.Items.Clear();
            LbxPattern.Items.SortDescriptions.Add(new SortDescription("", ListSortDirection.Ascending));
        }

        private void Button_AddAll_Click(object sender, RoutedEventArgs e)
        {

            itemGerichikRobot.Patterns.Clear();
            foreach (var item in data.DictionaryPattern)
            {
                itemGerichikRobot.Patterns.Add(item.Key);
                LbxTradePattern.Items.Add(item.Key);
            }

            LbxPattern.Items.Clear();
            LbxTradePattern.Items.SortDescriptions.Add(new SortDescription("", ListSortDirection.Ascending));

        }

        private void LbxPattern_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (((ListBox)sender).SelectedItem != null)
                foreach (var item in data.DictionaryPattern)
                {
                    if (((ListBox)sender).SelectedItem.ToString() == item.Key)
                    {
                        TxbxDescriptionPattern.Text = item.Value;
                    }
                }
        }

        private void LbxTradePattern_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (((ListBox)sender).SelectedItem != null)
                foreach (var item in data.DictionaryPattern)
                {
                    if (((ListBox)sender).SelectedItem.ToString() == item.Key)
                    {
                        TxbxDescriptionPattern.Text = item.Value;
                    }
                }
        }

        private void LbxPattern_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (((ListBox)sender).SelectedItem != null)
            {
                foreach (var item in data.DictionaryPattern)
                {
                    if (LbxPattern.SelectedItem.ToString() == item.Key)
                    {
                        itemGerichikRobot.Patterns.Add(item.Key);
                        break;
                    }
                }
                LbxTradePattern.Items.Add(LbxPattern.SelectedItem);
                LbxPattern.Items.Remove(LbxPattern.SelectedItem);
                LbxTradePattern.Items.SortDescriptions.Add(new SortDescription("", ListSortDirection.Ascending));
            }
        }

        private void LbxTradePattern_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (((ListBox)sender).SelectedItem != null)
            {
                foreach (var item in data.DictionaryPattern)
                {
                    if (LbxTradePattern.SelectedItem.ToString() == item.Key)
                    {
                        itemGerichikRobot.Patterns.Remove(item.Key);
                        break;
                    }
                }
                LbxPattern.Items.Add(LbxTradePattern.SelectedItem);
                LbxTradePattern.Items.Remove(LbxTradePattern.SelectedItem);
                LbxPattern.Items.SortDescriptions.Add(new SortDescription("", ListSortDirection.Ascending));
            }
        }
    }
}
